package com.atcumt.xiaoyuan;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class HJOD01 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }


    /**
     * 构建二叉树
     *
     * @param nums
     * @return
     */
    public static TreeNode createTreeNode(Integer[] nums) {
        if (nums.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(nums[0]);
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.add(root);
        int k = 1;
        while (!nodeQueue.isEmpty() && k < nums.length) {
            TreeNode curNode = nodeQueue.poll();
            if (nums[k] != null) {
                curNode.left = new TreeNode(nums[k]);
                nodeQueue.add(curNode.left);
                k++;
            }

            if (nums[k] != null && k < nums.length) {
                curNode.right = new TreeNode(nums[k]);
                nodeQueue.add(curNode.right);
                k++;
            }

        }
        return root;
    }
    public static int getMaxLen(TreeNode root) {
        if (root == null || root.val == -1) {
            return 0;
        }
        int left = getMaxLen(root.left);
        int right = getMaxLen(root.right);

        return root.val + Math.max(left, right);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] strArray = str.split(" ");
        Integer[] nums = new Integer[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            nums[i] = Integer.parseInt(strArray[i]);
        }

        TreeNode treeNode = createTreeNode(nums);
        int maxLen = getMaxLen(treeNode);
        System.out.println(maxLen);
    }

}
